Public Class frmDrawing
    Inherits System.Windows.Forms.Form

    Public mMove As New MouseMove
    Public Freeform As Boolean
    Private IconMenuPic As Control
    Private _Dirty As Boolean = False
    Public Property Dirty() As Boolean
        Get
            Return _Dirty
        End Get
        Set(ByVal Value As Boolean)
            _Dirty = Value
        End Set
    End Property

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer
    Friend WithEvents InkBox As MyInkPicture
    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents InkMenu As System.Windows.Forms.ContextMenu
    Friend WithEvents mnuClearInk2 As System.Windows.Forms.MenuItem
    Friend WithEvents mnuClearIcons2 As System.Windows.Forms.MenuItem
    Friend WithEvents MenuItem19 As System.Windows.Forms.MenuItem
    Friend WithEvents LN1 As System.Windows.Forms.MenuItem
    Friend WithEvents FF1 As System.Windows.Forms.MenuItem
    Friend WithEvents IconMenu As System.Windows.Forms.ContextMenu
    Friend WithEvents mnuDeleteIcon As System.Windows.Forms.MenuItem
    Friend WithEvents Timer1 As System.Windows.Forms.Timer
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.components = New System.ComponentModel.Container
        Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(frmDrawing))
        Me.InkBox = New NetworkWhiteboard.MyInkPicture
        Me.InkMenu = New System.Windows.Forms.ContextMenu
        Me.mnuClearInk2 = New System.Windows.Forms.MenuItem
        Me.mnuClearIcons2 = New System.Windows.Forms.MenuItem
        Me.MenuItem19 = New System.Windows.Forms.MenuItem
        Me.LN1 = New System.Windows.Forms.MenuItem
        Me.FF1 = New System.Windows.Forms.MenuItem
        Me.IconMenu = New System.Windows.Forms.ContextMenu
        Me.mnuDeleteIcon = New System.Windows.Forms.MenuItem
        Me.Timer1 = New System.Windows.Forms.Timer(Me.components)
        Me.SuspendLayout()
        '
        'InkBox
        '
        Me.InkBox.BackColor = System.Drawing.Color.White
        Me.InkBox.ContextMenu = Me.InkMenu
        Me.InkBox.InkEnabled = True
        Me.InkBox.Location = New System.Drawing.Point(0, 0)
        Me.InkBox.Name = "InkBox"
        Me.InkBox.Size = New System.Drawing.Size(292, 266)
        Me.InkBox.TabIndex = 1
        Me.InkBox.TabStop = False
        '
        'InkMenu
        '
        Me.InkMenu.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.mnuClearInk2, Me.mnuClearIcons2, Me.MenuItem19, Me.LN1, Me.FF1})
        '
        'mnuClearInk2
        '
        Me.mnuClearInk2.Index = 0
        Me.mnuClearInk2.Text = "Clear Ink"
        '
        'mnuClearIcons2
        '
        Me.mnuClearIcons2.Index = 1
        Me.mnuClearIcons2.Text = "Clear Icons"
        '
        'MenuItem19
        '
        Me.MenuItem19.Index = 2
        Me.MenuItem19.Text = "-"
        '
        'LN1
        '
        Me.LN1.Index = 3
        Me.LN1.RadioCheck = True
        Me.LN1.Text = "Draw Lines"
        '
        'FF1
        '
        Me.FF1.Checked = True
        Me.FF1.Index = 4
        Me.FF1.RadioCheck = True
        Me.FF1.Text = "Draw Freeform"
        '
        'IconMenu
        '
        Me.IconMenu.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.mnuDeleteIcon})
        '
        'mnuDeleteIcon
        '
        Me.mnuDeleteIcon.Index = 0
        Me.mnuDeleteIcon.Text = "Delete"
        '
        'frmDrawing
        '
        Me.AllowDrop = True
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.AutoScroll = True
        Me.ClientSize = New System.Drawing.Size(292, 266)
        Me.ContextMenu = Me.InkMenu
        Me.Controls.Add(Me.InkBox)
        Me.Icon = CType(resources.GetObject("$this.Icon"), System.Drawing.Icon)
        Me.Name = "frmDrawing"
        Me.ShowInTaskbar = False
        Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent
        Me.Text = "Untitled"
        Me.WindowState = System.Windows.Forms.FormWindowState.Maximized
        Me.ResumeLayout(False)

    End Sub

#End Region



    Private Sub frmDrawing_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
        mMove.Initialize(Timer1)
        'InkBox.InkBox.DefaultDrawingAttributes = Me.Parent.InkToolBar1.InkDrawingAttributes


    End Sub


    Private Sub InkBox_NewInAirPackets(ByVal sender As System.Object, ByVal e As Microsoft.Ink.InkCollectorNewInAirPacketsEventArgs) Handles InkBox.NewInAirPackets

        'TODO: Fix the eraser with the "new inair packets"
        Dim m As frmMain = Me.MdiParent
        If e.Cursor.Inverted Or m.InkToolBar1.Mode = UTE.InkBar.InkToolBarMode.Eraser Then
            InkBox.InkBox.EditingMode = Microsoft.Ink.InkOverlayEditingMode.Delete
        Else
            InkBox.InkBox.EditingMode = Microsoft.Ink.InkOverlayEditingMode.Ink
        End If

        If e.Cursor.Buttons.Item(0).State = Microsoft.Ink.CursorButtonState.Down Then
            Dim packetX As Long
            Dim packetY As Long
            'There may be multiple packets of data, but we will rely on
            'the fact that there will be at least one, and its first two
            'data items are the X and Y coordinates.
            packetX = e.PacketData(0)
            packetY = e.PacketData(1)
            Dim G As Graphics = CreateGraphics()
            Dim P As New Point(packetX, packetY)

            InkBox.InkBox.Renderer.InkSpaceToPixel(G, P)
            InkBox.ContextMenu.Show(InkBox, P)
        End If


    End Sub

    Private Sub InkBox_Stroke(ByVal sender As System.Object, ByVal e As Microsoft.Ink.InkCollectorStrokeEventArgs) Handles InkBox.Stroke
        _Dirty = True
        Dim NS As Microsoft.Ink.Stroke = e.Stroke
        If Freeform = False Then
            Dim DA As Microsoft.Ink.DrawingAttributes = e.Stroke.DrawingAttributes
            Dim s As Microsoft.Ink.Stroke = e.Stroke

            Dim Ps As New PerfectStroke
            Ps.AllowedVariation = 0.8
            NS = Ps.ForcePerfect(s)

            InkBox.InkBox.Ink.DeleteStroke(e.Stroke)
            NS = InkBox.InkBox.Ink.CreateStroke(NS.GetPoints)
            NS.DrawingAttributes = DA
            NS.DrawingAttributes.FitToCurve = False
            InkBox.InkBox.Renderer.Draw(Me.CreateGraphics(), NS)
            InkBox.InkBox.Ink.Dirty = True
            InkBox.Refresh()
        End If


    End Sub


    Private Sub ClearAll()
        Dirty = True
        InkBox.Controls.Clear()
        InkBox.InkBox.Ink.DeleteStrokes(InkBox.InkBox.Ink.Strokes)
        InkBox.Refresh()
    End Sub




    Private Sub mnuClearInk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuClearInk2.Click
        Dirty = True
        InkBox.InkBox.Ink.DeleteStrokes(InkBox.InkBox.Ink.Strokes)
        InkBox.Refresh()
    End Sub



    Private Sub mnuDeleteIcon_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuDeleteIcon.Click
        Dirty = True
        If Not IconMenuPic Is Nothing Then
            InkBox.Controls.Remove(IconMenuPic)
        End If

    End Sub

    Private Sub IconMenu_Popup(ByVal sender As Object, ByVal e As System.EventArgs) Handles IconMenu.Popup
        IconMenuPic = sender.sourcecontrol
    End Sub








    Private Sub mnuClearIcons_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuClearIcons2.Click
        Dirty = True
        InkBox.Controls.Clear()
    End Sub







    Private Sub frmDrawing_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Resize

        If Me.Height < Me.InkBox.MinY Then
            Me.InkBox.Height = Me.InkBox.MinY
        Else
            Me.InkBox.Height = Me.Height
        End If

        If Me.Width < Me.InkBox.MinX Then
            Me.InkBox.Width = Me.InkBox.MinX
        Else
            Me.InkBox.Width = Me.Width
        End If

        Me.AutoScroll = False
        Me.InkBox.Refresh()
        Me.Refresh()
        Me.AutoScroll = True
    End Sub

    Friend Sub UpdateFF()

        'FF.Checked = C.Freeform
        FF1.Checked = Freeform
        'LN.Checked = Not C.Freeform
        LN1.Checked = Not Freeform
    End Sub

    Private Sub LN1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LN1.Click
        Freeform = False
        Dim m As frmMain = Me.MdiParent
        m.UpdateFF()
        UpdateFF()
    End Sub

    Private Sub FF1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FF1.Click
        Freeform = True
        Dim m As frmMain = Me.MdiParent
        m.UpdateFF()
        UpdateFF()
    End Sub

    Private Sub frmDrawing_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles MyBase.DragDrop
        Dim m As frmMain = Me.MdiParent
        Dim Loc As Point = Me.PointToClient(New Point(e.X, e.Y))
        Dirty = True
        If e.Data.GetDataPresent(DataFormats.Text) Then
            Dim Icon As String = e.Data.GetData(DataFormats.Text).ToString()

            Select Case Icon
                Case "Text"
                    m.AddTextIcon(Loc)
                Case "Custom"
                    m.AddCustomIcon(Loc)
                Case Else
                    m.AddIcon(Icon, Loc)
            End Select
        ElseIf e.Data.GetDataPresent(DataFormats.FileDrop) Then
            Dim files As String() = CType(e.Data.GetData(DataFormats.FileDrop), String())
            Try
                ' Assign the first image to the 'picture' variable.

                Dim Image As Bitmap = Image.FromFile(files(0))
                ' Set the picture location equal to the drop point.
                m.AddIcon(Image, Loc)
            Catch ex As Exception
                MessageBox.Show(ex.Message)
                Return
            End Try

        ElseIf e.Data.GetDataPresent(DataFormats.Bitmap) Then
            Try
                ' Create an Image and assign it to the picture variable.
                Dim Image As Bitmap = CType(e.Data.GetData(DataFormats.Bitmap), Image)
                ' Set the picture location equal to the drop point.
                m.AddIcon(image, Loc)
            Catch ex As Exception
                MessageBox.Show(ex.Message)
                Return
            End Try


        End If

    End Sub

    Private Sub frmDrawing_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles MyBase.DragEnter
        If e.Data.GetDataPresent(DataFormats.Text) Or e.Data.GetDataPresent(DataFormats.FileDrop) Or e.Data.GetDataPresent(DataFormats.Bitmap) Then
            ' There is text. Allow copy.
            e.Effect = DragDropEffects.Copy

        Else
            ' There is no text. Prohibit drop.
            e.Effect = DragDropEffects.None
        End If
    End Sub




End Class
